package cn.hg.solon.youcan.system.provider;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.db.PageResult;
import org.noear.solon.annotation.ProxyComponent;
import org.noear.solon.data.annotation.Tran;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.api4j.select.Queryable;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.easy.query.solon.annotation.Db;

import cn.hg.solon.youcan.common.constant.WebConstants;
import cn.hg.solon.youcan.system.entity.EqUserLogin;
import cn.hg.solon.youcan.system.entity.UserLogin;
import cn.hg.solon.youcan.system.service.UserLoginService;

/**
 * @author 胡高
 * @date 2023/08/03
 */
@ProxyComponent
public class EqUserLoginProvider implements UserLoginService {

    @Db("db1")
    private EasyQuery easyQuery;

    private Queryable<EqUserLogin> buildQuery(Map<String, Object> map) {
        String word = (String)map.get("word");
        String status = (String)map.get("status");
        Date startDate = (Date)map.get("startDate");
        Date endDate = (Date)map.get("endDate");
        String sortField = (String)map.get("sortField");
        String sortType = (String)map.get("sortType");

        boolean customOrder = StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType);

        // FROM sys_operate_log AS t
        return this.easyQuery.queryable(EqUserLogin.class)
            // WHERE AND t.`status` = ${status} AND ${startDate} <= t.`action_datetime` AND t.`action_datetime` <= ${endDate}
            .where(w -> w.eq(StrValidator.isNotBlank(status), EqUserLogin::getStatus, status)
                .rangeClosed(EqUserLogin::getLoginDatetime, ObjUtil.isNotNull(startDate), startDate, ObjUtil.isNotNull(endDate), endDate)
                //  AND (t.`title` LIKE '%${word}%' OR t.`user_name` LIKE '%${word}%' OR t.`dept_name` LIKE '%${word}%'
                //       OR t.`url` LIKE '%${word}%' OR t.`ip` LIKE '%${word}%' OR t.`location` LIKE '%${word}%'
                //       OR t.`param` LIKE '%${word}%' OR t.`result` LIKE '%${word}%' OR t.`message` LIKE '%${word}%')
                .and(StrValidator.isNotBlank(word),
                    a -> a.like(EqUserLogin::getAccount, word)
                    .or().like(EqUserLogin::getNickname, word)
                    .or().like(EqUserLogin::getIp, word)
                    .or().like(EqUserLogin::getLocation, word)
                    .or().like(EqUserLogin::getBrowser, word)
                    .or().like(EqUserLogin::getOs, word)
                    .or().like(EqUserLogin::getMsg, word)
                    ))
            // ORDER BY ${sortField} DESC
            .orderBy(customOrder, ob -> ob.sqlNativeSegment(CharSequenceUtil.toUnderlineCase(sortField)),
                !CharSequenceUtil.equalsIgnoreCase(sortType, WebConstants.ORDER_DIRECTION_DESC));
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.UserLoginService#clear()
     */
    @Tran
    @Override
    public boolean clear() {
        return this.easyQuery.deletable(EqUserLogin.class).allowDeleteStatement(true).executeRows() > 0L;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.UserLoginService#delete(java.util.List)
     */
    @Tran
    @Override
    public boolean delete(List<Integer> idList) {
        return this.easyQuery.deletable(EqUserLogin.class)
            .where(w -> w.in(EqUserLogin::getId, idList))
            .executeRows() > 0;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.UserLoginService#get(java.lang.Integer)
     */
    @Override
    public UserLogin get(Integer id) {
        return this.easyQuery.queryable(EqUserLogin.class).whereById(id).firstOrNull();
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.UserLoginService#insert(cn.hg.solon.youcan.system.entity.UserLogin)
     */
    @Tran
    @Override
    public boolean insert(UserLogin bean) {
        EqUserLogin cloneBean = BeanUtil.copyProperties(bean, EqUserLogin.class);

        return this.easyQuery.insertable(cloneBean).executeRows() > 0;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.UserLoginService#pageBy(int, int, java.util.Map)
     */
    @Override
    public PageResult<? extends UserLogin> pageBy(int pageNumber, int pageSize, Map<String, Object> paraMap) {
        EasyPageResult<EqUserLogin> pageList = this.buildQuery(paraMap).toPageResult(Convert.toLong(pageNumber), pageSize);

        PageResult<EqUserLogin> result = new PageResult<>();
        result.addAll(pageList.getData());
        result.setTotal(Convert.toInt(pageList.getTotal()));

        return result;
    }

}
